
from rest_framework.response import Response
from django.contrib.sessions.models import Session
from love.settings import r
def session_auth(func):
    def inner(self,request,*args):
        user=request.session.get('user_id')
        print(user)
        print(request.session)
        if not user:
            return Response({'code':4014})
        else:
            session_key = request.session.session_key
            print(session_key, '666')
            key=r.get('session:'+session_key)
            print(key)
            print(dir(session_key))
            print(dir(key.decode()))
            """
            查出所有不属于当前浏览器会话的session_key，如果当前user_id出现在以前的session会话中，则删除
            """
            for session in Session.objects.exclude(session_key=session_key):
                data = session.get_decoded()
                if data.get('user_id', None) == request.session.get('user_id', None):
                    session.delete()
            res = func(self, request)
            return res

    return inner






